package org.pps.gl.client.vue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.pps.gl.client.GetInfoService;
import org.pps.gl.client.GetInfoServiceAsync;
import org.pps.gl.client.LoginInfo;
import org.pps.gl.client.data.UserInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RDC_Recherche extends Composite {
	final SimplePanel main;
	VerticalPanel recherche;
	final VerticalPanel resultat;
	final SuggestBox textBoxEmail, textBoxNomPrenom, textBoxSociete;
	final SuggestBox textBoxEcole;

	public RDC_Recherche() {
		main = new SimplePanel();
		recherche = new VerticalPanel();
		recherche.setStyleName("profilMarge");
		Label titre = new Label("Recherche par critères");
		titre.setStylePrimaryName("titre");
		recherche.add(titre);

		FlexTable flexTable = new FlexTable();
		flexTable.setCellPadding(2);
		recherche.add(flexTable);
		recherche.setCellHorizontalAlignment(flexTable,
				HasHorizontalAlignment.ALIGN_LEFT);
		recherche.setCellVerticalAlignment(flexTable,
				HasVerticalAlignment.ALIGN_TOP);

		Label lblEmail = new Label("Email");
		flexTable.setWidget(0, 0, lblEmail);
		flexTable.getCellFormatter().setHeight(0, 0, "30px");
		flexTable.getCellFormatter().setWidth(0, 0, "200px");
		lblEmail.setSize("200px", "30px");

		MultiWordSuggestOracle oracleM = new MultiWordSuggestOracle();
		ArrayList<String> email = LoginInfo.getCurrentUser().getAllEmail();
		if (email != null)
			oracleM.addAll(email);
		textBoxEmail = new SuggestBox(oracleM);
		// textBoxEmail = new TextBox();
		flexTable.setWidget(0, 1, textBoxEmail);
		flexTable.getCellFormatter().setHeight(0, 1, "35px");
		flexTable.getCellFormatter().setWidth(0, 1, "200px");
		textBoxEmail.setWidth("200px");

		Label lblNomPrenom = new Label("Nom/Prenom");
		flexTable.setWidget(1, 0, lblNomPrenom);
		flexTable.getCellFormatter().setHeight(1, 0, "30px");
		flexTable.getCellFormatter().setWidth(1, 0, "200px");
		lblNomPrenom.setSize("200px", "30px");

		MultiWordSuggestOracle oracleN = new MultiWordSuggestOracle();
		ArrayList<String> nom = LoginInfo.getCurrentUser().getAllNom();
		if (nom != null)
			oracleN.addAll(nom);
		textBoxNomPrenom = new SuggestBox(oracleN);
		// textBoxNomPrenom = new TextBox();
		flexTable.setWidget(1, 1, textBoxNomPrenom);
		flexTable.getCellFormatter().setHeight(1, 1, "35px");
		flexTable.getCellFormatter().setWidth(1, 1, "200px");
		textBoxNomPrenom.setWidth("200px");

		Label lblSociete = new Label("Société");
		flexTable.setWidget(2, 0, lblSociete);
		flexTable.getCellFormatter().setHeight(2, 0, "30px");
		flexTable.getCellFormatter().setWidth(2, 0, "200px");
		lblSociete.setSize("200px", "30px");

		MultiWordSuggestOracle oracleE = new MultiWordSuggestOracle();
		ArrayList<String> enterprise = LoginInfo.getCurrentUser()
				.getAllEntreprise();
		if (enterprise != null)
			oracleE.addAll(enterprise);
		textBoxSociete = new SuggestBox(oracleE);
		// textBoxSociete = new TextBox();
		flexTable.setWidget(2, 1, textBoxSociete);
		flexTable.getCellFormatter().setHeight(2, 1, "35px");
		flexTable.getCellFormatter().setWidth(2, 1, "200px");
		textBoxSociete.setWidth("200px");

		Label lblEcole = new Label("Ecole/Spécialité");
		flexTable.setWidget(3, 0, lblEcole);
		flexTable.getCellFormatter().setHeight(3, 0, "30px");
		flexTable.getCellFormatter().setWidth(3, 0, "200px");
		lblEcole.setSize("200px", "30px");

		MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
		ArrayList<String> schools = LoginInfo.getCurrentUser().getAllSchools();
		ArrayList<String> spe = LoginInfo.getCurrentUser().getAllSpecialite();
		if (schools != null)
			oracle.addAll(schools);
		if (spe != null)
			oracle.addAll(spe);
		textBoxEcole = new SuggestBox(oracle);
		flexTable.setWidget(3, 1, textBoxEcole);
		flexTable.getCellFormatter().setHeight(3, 1, "35px");
		flexTable.getCellFormatter().setWidth(3, 1, "200px");
		textBoxEcole.setWidth("200px");

		Button go = new Button("Rechercher");
		flexTable.setWidget(4, 1, go);

		resultat = new VerticalPanel();
		resultat.setStyleName("profilMarge");
		main.add(recherche);
		go.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				GetInfoServiceAsync gis = GWT.create(GetInfoService.class);
				gis.doSearch(LoginInfo.getCurrentUser().getEmailAddress(),
						textBoxEmail.getValue(), textBoxNomPrenom.getValue(),
						textBoxSociete.getValue(), textBoxEcole.getValue(),
						new AsyncCallback<HashMap<UserInfo, Integer>>() {
							@Override
							public void onFailure(Throwable caught) {
								
							}

							@Override
							public void onSuccess(
									HashMap<UserInfo, Integer> result) {
								resultat.clear();
								Label titre = new Label(
										"Résultats de la recherche");
								titre.setStylePrimaryName("titre");
								resultat.add(titre);
								if (result.isEmpty()) {
									Label line = new Label("Aucun résultat");
									resultat.add(line);
								} else {
									final List<Entry<UserInfo, Integer>> entries = new ArrayList<Entry<UserInfo, Integer>>(
											result.entrySet());
									for (Entry<UserInfo, Integer> u : entries) {
										HorizontalPanel line = new HorizontalPanel();
										line.setSpacing(5);
										// lien vers le profil
										Hyperlink hyperlink = new Hyperlink(
												"",
												"user/"
														+ u.getKey()
																.getEmailAddress());
										hyperlink.setStyleName("ficheHyper");
										hyperlink
												.setStylePrimaryName("ficheHyper");
										line.add(hyperlink);
										hyperlink.setSize("33px", "30px");
										// nom de la personne
										Label label = new Label(u.getKey()
												.getFirstname()
												+ " "
												+ u.getKey().getLastname()
												+ " ");
										label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
										line.add(label);
										line.setCellHeight(label, "10px");
										line.setCellVerticalAlignment(
												label,
												HasVerticalAlignment.ALIGN_MIDDLE);
										label.setSize("170px", "20px");
										Label distance = new Label("");
										if (u.getValue() == 0)
											distance.setText("moi");
										if (u.getValue() == 1)
											distance.setText("contact");
										if ((u.getValue() > 1) && (u.getValue() < 9)) {
											distance.setText(u.getValue()
													.toString() + "ème niveau");
										}
										distance.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
										line.add(distance);
										line.setCellHeight(distance, "10px");
										line.setCellVerticalAlignment(
												distance,
												HasVerticalAlignment.ALIGN_MIDDLE);
										distance.setSize("100px", "20px");

										resultat.add(line);
										line.setSize("168px", "27px");
									}
								}
								Button retour = new Button("Retour");
								resultat.add(retour);
								retour.addClickHandler(new ClickHandler() {
									@Override
									public void onClick(ClickEvent event) {
										main.clear();
										main.add(recherche);
									}
								});
								main.clear();
								main.add(resultat);
							}
						});
			}
		});
		initWidget(main);
	}
}
